package com.work.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.work.entity.Basestring;
import com.work.entity.Images;
import com.work.entity.Setting;
import com.work.entity.User;
import com.work.service.BasestringService;
import com.work.service.ImagesService;
import com.work.service.SettingService;
import com.work.service.UserService;
import com.work.utils.RedisUtils;
import com.work.utils.ResultUtils;
import com.work.utils.ResultVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/mall")
public class HarmonyUserController {

    @Resource
    private ImagesService imagesService;

    @Resource
    private BasestringService basestringService;

    @Resource
    private SettingService settingService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /*
    * 获取Home图片
    * */
    @PostMapping("/images")
    public ResultVo getimages(@RequestBody Images images){
        System.out.println("1111");
        String redisList = stringRedisTemplate.opsForValue().get(RedisUtils.CACHE_IMAGE_LIST);
        if(redisList == null) {
            List<Images> list = imagesService.list();
            String redisData = JSONObject.toJSONString(list);
            stringRedisTemplate.opsForValue().set(RedisUtils.CACHE_IMAGE_LIST , redisData);
            return ResultUtils.success("成功拿到图片数据", list);
        }else {
            List list = JSONObject.parseObject(redisList, List.class);
            ResultUtils.success("查询成功",list);
        }
        return ResultUtils.succcess();
    }

    /*
    * 获取setting图片
    * */
    @PostMapping("/setting")
    public ResultVo getSetting(@RequestBody Setting setting){
        List<Setting> list = settingService.list();
        return ResultUtils.success("成功拿到图片数据",list);
    }

    /*
    * 获取Home文本信息*/
    @PostMapping("/text")
    public ResultVo gettext(@RequestBody Basestring basestring){
        List<Basestring> list = basestringService.list();
        return ResultUtils.success("获取成功",list);
    }
}
